package com.ruoyi.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class ExcelUtil {

    public static List getExcelData(String path,int startNum ,Class classes, Map<String , String > columnMap) throws Exception{
        List<Object> list = new ArrayList<>();
        //用流去读取读取数据
        Map<String,Object> map = new HashMap<>();
        List<String> headerList = new ArrayList<>();
        String[] split = path.split(",");
        for (int i = 0; i < split.length; i++) {
            if (StringUtils.isBlank(split[i])){
                continue;
            }
            String extString = split[i].substring(path.lastIndexOf("."));
            InputStream is = new FileInputStream(split[i]);
            Workbook wb = null;
            if("xls".equals(extString)||".xls".equals(extString)){
                wb = new HSSFWorkbook(is);
            }else if("xlsx".equals(extString)||".xlsx".equals(extString)){
                wb = new XSSFWorkbook(is);
            }
            Sheet sheet= wb.getSheetAt(0);
            int rowNum = sheet.getLastRowNum();//行数
            for (int j = startNum; j <= rowNum; j++) {
//                HSSFRow rows = sheets.getRow(j);//单元格数
                Row row=sheet.getRow(j);
                if(row!=null){
                    int rowLength = row.getLastCellNum();
                    if(j==startNum) {
                        for (int k = 0; k < rowLength; k++) {
                            System.out.println(row.getCell(k));
                            headerList.add(row.getCell(k).toString());
                        }
                        map.put("headerList",headerList);
                    }else if(j>startNum){
                            Object obj = classes.newInstance();
                            for (int k = 0; k < headerList.size(); k++) {
                                if (row.getCell(k) != null) {
                                    String value = columnMap.get(headerList.get(k));
                                    if (value==null){
                                        continue;
                                    }
                                    try {
                                        row.getCell(k).setCellType(Cell.CELL_TYPE_STRING);
                                        setProperty(obj, value, row.getCell(k).getStringCellValue());
                                    }catch (Exception e){
                                        System.out.println(e.toString());
                                    }

                                }
                            }
                        list.add(obj);
                    }
                }
            }
        }
        return list;
    }

    /** 该方法用于传入某实例对象以及对象方法名、修改值，通过放射调用该对象的某个set方法设置修改值 */
    public static void setProperty(Object beanObj,String fieldName,  Object value){
        if (beanObj == null || StringUtils.isEmpty(fieldName) || value == null || value.toString().length() == 0){
            return;
        }
        try {
            PropertyDescriptor pd = new PropertyDescriptor(fieldName, beanObj.getClass());
            Method setMethod = pd.getWriteMethod();
            List<Object> params = new ArrayList<>();
            Class<?>[] parameterTypes = setMethod.getParameterTypes();
            for (Class clazz : parameterTypes){
                if (clazz == int.class || clazz == Integer.class){
                    params.add(Integer.valueOf(value.toString()));
                }else if (clazz == double.class || clazz == Double.class){
                    params.add(Double.valueOf(value.toString()));
                }else if(clazz == float.class || clazz == Float.class){
                    params.add(Float.valueOf(value.toString()));
                }else if (clazz == String.class){
                    params.add(value.toString());
                }else if(clazz == boolean.class || clazz == Boolean.class){
                    params.add(Boolean.valueOf(value.toString()));
                }else if (clazz == char.class){
                    params.add(value.toString().getBytes()[0]);
                }
            }
            //invoke是执行set方法
            setMethod.invoke(beanObj, params.toArray());

        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
    }

}